package com.dongjiantong.farmrecords.model;
/**
 * 免疫记录表
 */

import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import lombok.Data;

import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;
import java.util.List;

@Table(name = "farmrecords_immunization")
@Data
public class FarmRecordsImmunization extends BaseEntity {
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();
    private static final long serialVersionUID = -1306740054170826402L;

    /**
     *  单据编号
     */
    private String number;
    /**
     * 关联牛档案id
     */
    private Long cow_record_id;
    /**
     * 牛号
     */
    private String cow_number;
    /**
     *  状态
     */
    private Integer status;
    /**
     *  养殖场id
     */
    private Long farm_id;
    /**
     *  养殖场
     */
    private String farm_name;
    /**
     * 省id
     */
    private Integer province_id;

    /**
     * 市id
     */
    private Integer city_id;

    /**
     * 县id
     */
    private Integer region_id;

    /**
     * 镇id
     */
    private Integer street_id;

    /**
     * 村id
     */
    private Integer village_id;
    /**
     *  创建单据的人id
     */
    private Long user_id;
    /**
     *  创建单据的人name
     */
    private String user_name;
    /**
     *  二级
     */
    private Integer breed_second_type;
    /**
     *  单位
     */
    private Integer breed_unit;
    /**
     *  存栏
     */
    private Integer stock;
    /**
     *  圈舍面积
     */
    private Float acreage;
    /**
     *  耳标号
     */
    @JsonIgnore
    private String ear_number_json;
    @Transient
    private List<String> ear_number_list;

    /**
     * 耳标备份
     */
    @JsonIgnore
    @Transient
    private String original_ear_json;
    @Transient
    private List<String> original_ear;

    /**
     *  单据是否有效
     */
    private Integer delete_flag;
    /**
     *  创建日期
     */
    private Date created_at;
    /**
     *  更新日期
     */
    private Date updated_at;


    /**
     * 关联的免疫记录子表的信息
     */
    @Transient
    private List<FarmRecordsImmunizationSub> vaccines;

    /**
     * 关联的文件表图片list
     *//*
    @Transient
    private List picList;

    *//**
     * 关联的文件表视频list
     *//*
    @Transient
    private List videoList;*/

    public void setEar_number_json(String ear_number_json) {
        this.ear_number_json = ear_number_json;

        if (Strings.isNullOrEmpty(ear_number_json)) {
            this.ear_number_list = null;
        } else {
            try {
                this.ear_number_list = objectMapper.readValue(ear_number_json, new TypeReference<List<String>>() {});
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setEar_number_list(List<String> ear_number_list) {
        this.ear_number_list = ear_number_list;

        if (ear_number_list == null || ear_number_list.isEmpty()) {
            this.ear_number_json = "[]";
        }else {
            try {
                this.ear_number_json = objectMapper.writeValueAsString(ear_number_list);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setOriginal_ear_json(String original_ear_json) {
        this.original_ear_json = original_ear_json;

        if (Strings.isNullOrEmpty(original_ear_json)) {
            this.original_ear = null;
        } else {
            try {
                this.original_ear = objectMapper.readValue(original_ear_json, new TypeReference<List<String>>() {});
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setOriginal_ear(List<String> original_ear) {
        this.original_ear = original_ear;

        if (original_ear == null || original_ear.isEmpty()) {
            this.original_ear_json = "[]";
        }else {
            try {
                this.original_ear_json = objectMapper.writeValueAsString(original_ear);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }
}